package com.cma.cmabackend.service;

import com.cma.cmabackend.dao.StandardDao;
import com.cma.cmabackend.entity.Parameter;
import com.cma.cmabackend.entity.Standard;
import com.cma.cmabackend.entity.re.ReStandardPara;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class StandardService {
    @Resource
    CategoryService categoryService;
    @Resource
    TypeService typeService;

    @Resource
    StandardDao standardDao;

    public Standard add(String category, String type, String name, String file,String iid) {
        Standard newStandard = new Standard();
        newStandard.setCategoryId(categoryService.getCategoryIdOrAdd(category));
        newStandard.setTypeId(typeService.getTypeIdOrAdd(type, category));
        newStandard.setName(name);
        newStandard.setFileUuid(file);
        newStandard.setIid(iid);
        newStandard.setParameterList(new ArrayList<>());
        return standardDao.save(newStandard);
    }

    public Standard findById(Integer id) {
        return standardDao.findById(id).orElse(null);
    }

    public Standard save(Standard x) {
        return standardDao.save(x);
    }

    public Standard findByParameter(Parameter p) {
        return standardDao.findByParameterListContains(p);
    }

    public ReStandardPara getPara(Integer id) {
        Standard standard = standardDao.findById(id).orElse(null);
        if (standard == null) {
            return new ReStandardPara();
        }
        return new ReStandardPara(standard.getId(), standard.getSample(), standard.getSop(), standard.getSimulation());
    }
}
